package com.bingo.controller;

import com.bingo.common.R;
import com.bingo.entity.HisPool;
import com.bingo.entity.dto.req.ReqDivide;
import com.bingo.entity.dto.req.ReqForm;
import com.bingo.entity.dto.req.ReqFormSite;
import com.bingo.service.DivideService;
import com.bingo.service.HisPoolService;
import com.bingo.until.IsToday;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/divide")
public class DivideController {
    private DivideService divideService;
    @Autowired
    public void setDivideService(DivideService divideService) {
        this.divideService = divideService;
    }

    private HisPoolService hisPoolService;
    @Autowired
    public void setHisPoolService(HisPoolService hisPoolService) {
        this.hisPoolService = hisPoolService;
    }

    //新增分塘记录
    @PostMapping("/report")
    public R Divide(@RequestBody ReqDivide reqDivide){

        if (!IsToday.isToday(reqDivide.getDate())){
            HisPool pool = hisPoolService.getPoolByNo(reqDivide.getPoolNoIn());
            if (pool == null){
                return R.error("不存在该分进池塘!");
            }
        }

        if (!IsToday.isToday(reqDivide.getDate())){
            HisPool pool = hisPoolService.getPoolByNo(reqDivide.getPoolNoOut());
            if (pool == null || pool.getType() == 0){
                return R.error("不存在该分出池塘或为空塘!");
            }
        }

        return divideService.add(reqDivide.getPoolNoOut(),reqDivide.getPoolNoIn(),reqDivide.getQuantity(),reqDivide.getWeight(),reqDivide.getDate(),reqDivide.getUserId());
    }

    //查询分塘记录
    @PostMapping
    public R Divide(@RequestBody ReqForm reqForm){
        return R.success(divideService.selectEveryDayDivide(reqForm.getPoolNos(),reqForm.getDate()[0],reqForm.getDate()[1]));
    }

    //场的分塘记录
    @PostMapping("/site")
    public R getSiteDivide(@RequestBody ReqFormSite ReqFormSite){
        return R.success(divideService.selectSiteDivide(ReqFormSite.getSiteNos(),ReqFormSite.getDate()[0],ReqFormSite.getDate()[1]));
    }

    @PostMapping("/record")
    public R divideRecord(@RequestBody ReqForm reqForm) {
        return R.success(divideService.selectDivideRecord(reqForm.getPoolNos(), reqForm.getDate()[0], reqForm.getDate()[1],reqForm.getUserId()));
    }
}
